* Change directory to where the folder with the data is
cd "Data Archive TWT Predictions\DSBC"

use baseNopressureBars, clear 
*Repeat for the other 4 conditions

sort RTordered
*Merge Estimated CDFs
merge 1:1 subj trial using LeftChoiceNopressureBars, nogenerate
merge 1:1 subj trial using RightChoiceNopressureBars, nogenerate

*Calculate choice proportions
egen meanleft= mean(choiceleft), by(subject idpair)
sum meanleft

*Re-name variables to highlight what stimuli were displayed
 local k = 1
while `k' < 61{
capture noisily{
gen cdfAsubj`k'left3right1=cdfAsubj`k'id1 
gen cdfAsubj`k'left5right1=cdfAsubj`k'id2
gen cdfAsubj`k'left7right1=cdfAsubj`k'id3
gen cdfAsubj`k'left9right1=cdfAsubj`k'id4 
gen cdfAsubj`k'left5right3=cdfAsubj`k'id5 
gen cdfAsubj`k'left7right3=cdfAsubj`k'id6 
gen cdfAsubj`k'left9right3=cdfAsubj`k'id7 
gen cdfAsubj`k'left7right5=cdfAsubj`k'id8 
gen cdfAsubj`k'left9right5=cdfAsubj`k'id9 
gen cdfAsubj`k'left9right7=cdfAsubj`k'id10 
gen cdfAsubj`k'left4right2=cdfAsubj`k'id11 
gen cdfAsubj`k'left5right2=cdfAsubj`k'id12
gen cdfAsubj`k'left6right2=cdfAsubj`k'id13  
gen cdfAsubj`k'left8right2=cdfAsubj`k'id14  
gen cdfAsubj`k'left5right4=cdfAsubj`k'id15  
gen cdfAsubj`k'left6right4=cdfAsubj`k'id16  
gen cdfAsubj`k'left8right4=cdfAsubj`k'id17 
gen cdfAsubj`k'left6right5=cdfAsubj`k'id18 
gen cdfAsubj`k'left8right5=cdfAsubj`k'id19 
gen cdfAsubj`k'left8right6=cdfAsubj`k'id20 

gen cdfBsubj`k'left3right1=cdfBsubj`k'id1 
gen cdfBsubj`k'left5right1=cdfBsubj`k'id2
gen cdfBsubj`k'left7right1=cdfBsubj`k'id3
gen cdfBsubj`k'left9right1=cdfBsubj`k'id4 
gen cdfBsubj`k'left5right3=cdfBsubj`k'id5 
gen cdfBsubj`k'left7right3=cdfBsubj`k'id6 
gen cdfBsubj`k'left9right3=cdfBsubj`k'id7 
gen cdfBsubj`k'left7right5=cdfBsubj`k'id8 
gen cdfBsubj`k'left9right5=cdfBsubj`k'id9 
gen cdfBsubj`k'left9right7=cdfBsubj`k'id10 
gen cdfBsubj`k'left4right2=cdfBsubj`k'id11 
gen cdfBsubj`k'left5right2=cdfBsubj`k'id12
gen cdfBsubj`k'left6right2=cdfBsubj`k'id13  
gen cdfBsubj`k'left8right2=cdfBsubj`k'id14  
gen cdfBsubj`k'left5right4=cdfBsubj`k'id15  
gen cdfBsubj`k'left6right4=cdfBsubj`k'id16  
gen cdfBsubj`k'left8right4=cdfBsubj`k'id17 
gen cdfBsubj`k'left6right5=cdfBsubj`k'id18 
gen cdfBsubj`k'left8right5=cdfBsubj`k'id19 
gen cdfBsubj`k'left8right6=cdfBsubj`k'id20 

}
local k = `k' + 1
}

 *Loop over subjects and idpairs to create percentiles of the CDF related to the relative choice frequencies
  local k = 1
          while `k' < 61{
 local i = 1
          while `i' < 10{	

gen meanleftsubj`k'with5and`i'=meanleft if idleft==5 & idright==`i'
sum meanleftsubj`k'with5and`i'
replace meanleftsubj`k'with5and`i'=r(mean)

capture noisily: gen meanleftsubj`k'with`i'and5=meanleft if idleft==`i' & idright==5
sum meanleftsubj`k'with`i'and5
replace meanleftsubj`k'with`i'and5=r(mean)
local i = `i' + 1
            }   			
local k = `k' + 1
}


gen tmeank=.
 local m = 1
          while `m' < 61{
 local j = 1
          while `j' < 11{
 local i = 1
          while `i' < 11{

	*get percentiles
capture noisily gen ttlow=RTordered if  meanleftsubj`m'with`j'and`i'>0.5 & 0.5/(meanleftsubj`m'with`j'and`i')>=cdfAsubj`m'left`j'right`i'
capture noisily	gen tthigh=RTordered if  meanleftsubj`m'with`j'and`i'>0.5 & 0.5/(meanleftsubj`m'with`j'and`i')<=cdfAsubj`m'left`j'right`i'

capture noisily replace ttlow=RTordered if  meanleftsubj`m'with`j'and`i'<0.5 & 0.5/(1- meanleftsubj`m'with`j'and`i')>=cdfBsubj`m'left`j'right`i'
capture noisily replace tthigh=RTordered if  meanleftsubj`m'with`j'and`i'<0.5 & 0.5/(1- meanleftsubj`m'with`j'and`i')<=cdfBsubj`m'left`j'right`i'

capture noisily	egen tlow=max(ttlow)
capture noisily egen thigh=min(tthigh)
		
	*collapse new variables 
capture noisily gen tmeanksubj`m'left`j'right`i'=(tlow + thigh)/2
capture noisily sum tmeanksubj`m'left`j'right`i' 
capture noisily replace tmeanksubj`m'left`j'right`i'=r(mean) if subj==`m'
capture noisily	replace tmeank=tmeanksubj`m'left`j'right`i' if subj==`m'
				
*drop variables only used for the loop
capture noisily	drop ttlow tthigh tlow thigh
local i = `i' + 1
            }   
local j = `j' + 1
			}
local m = `m' + 1
}

save percentilesNopressureBar, replace 

********************************************
* Take care of changing the name of the saved datasets between the four conditions!
********************************************

use percentilesNopressureBar, clear

*Loop over the cases described in Theorem 2 (Alos-Ferrer, Fehr, and Netzer (2021), Theorem 3).
 local i = 1
          while `i' < 21{
	gen compak`i'=.
	gen compbk`i'=.
local i = `i' + 1
            }   

 local m = 1
          while `m' < 61{			
	local i = 1
          while `i' < 10{	
		local j = 1
          while `j' < 10{		  
	capture: gen pbarksubj`m'left`j'right`i'=.
    capture: gen pbarksubj`m'left`i'right`j'=. 
	local j = `j' + 1
            }  
    local i = `i' + 1
            }  
local m = `m' + 1   
}			
	
 local m = 1
          while `m' < 61{
 local i = 1
          while `i' < 10{			  
 local k = 1
          while `k' < 10{		  
		
		*case 1: py(`k')=1/2
		*if y is indifferent to the reference then x should have the same prop than with the ref.
		*gen pbark=py`i'`j' if  py`i'`k'==0.5
		*j is 5 the reference
		replace pbarksubj`m'left`k'right`i'= 1-meanleftsubj`m'with5and`k' if  meanleftsubj`m'with5and`i'==0.5

		
		*case 2: py(`k')>1/2, when they are both >1/2.
		*py`i'`j' : prop x over y;  FY`i'`j' CDF of x over z;  ref= entire dataset ordered RTs of reference (the grid); 
		
		*find the RT of y over z that corresponds to the percentile in the formula.
		*to do this use all values that are smaller than all RTs in the entire dataset and take the max that fulfills the condition 
		* then take the value of the CDF that corresponds to that value of the ordered RT and multiply it by the prop of x over z choices.  
		*gen hlpv1=py`i'`j'*FY`i'`j' if tmeank`i'`k'>=ref
		capture noisily{
		gen hlpv1= meanleftsubj`m'with5and`k' * cdfAsubj`m'left5right`k' if tmeanksubj`m'left5right`i'>=RTordered
		egen hlpv2=max(hlpv1) 
		***  predicted value for >1/2
		replace pbarksubj`m'left`k'right`i'=hlpv2 if  meanleftsubj`m'with5and`i'>0.5
		drop hlpv2 hlpv1
		}
		
		*case 3: py(`k')<1/2, when both smaller than 1/2.
	    *gen hlpv1=pn`i'`j'*FN`i'`j' if tmeank`i'`k'>=ref
		capture noisily{
		gen hlpv1= meanleftsubj`m'with5and`k' * cdfBsubj`m'left5right`k' if tmeanksubj`m'left5right`i'>=RTordered
		egen hlpv2=max(hlpv1) 
		replace pbarksubj`m'left`k'right`i'=1-hlpv2 if  meanleftsubj`m'with5and`i'<0.5
				drop hlpv1 hlpv2
		}
							
local k = `k' + 1
}   		
local i = `i' + 1
}   
local m = `m' + 1   
}
save TWTPredictionsTh3NopressureBar, replace 
********************************************
* Take care of changing the name of the saved datasets between the four conditions!
********************************************

use TWTPredictionsTh3NopressureBar, clear
*Merge estimated risk attitudes
merge m:1 subj using riskattitudesCARA, nogen
merge m:1 subj using riskattitudesCRRA, nogen
tab choiceleft
egen m1 = max(out1)
egen m2 = max(out2)
gen maxOut=max(m1,m2)

*Generate choice proportions from estimated risk attitudes
gen RUMCRRAleft = normal(( (prob1 * out1^rhatCRRA_NoP_Bars) -(prob2 * out2^rhatCRRA_NoP_Bars)) /noiseCRRA_NoP_Bars)
gen RUMCARAleft = normal((((prob1*(1-exp(-rhatCARA_NoP_Bars*out1))/(1-exp(-rhatCARA_NoP_Bars*(maxOut))))-(prob2*(1-exp(-rhatCARA_NoP_Bars*out2))/(1-exp(-rhatCARA*(maxOut)))) /noiseCARA_NoP_Bars)))
gen RPMCRRAleft = normal((rhatCRRA_NoP_Bars -rstarcrra) /noise_NoP_Bars) if prob1>prob2
replace RPMCRRAleft = 1-normal((rhatCRRA_NoP_Bars -rstarcrra) /noise_NoP_Bars) if prob1<prob2
gen RPMCARAleft = normal((rhatCARA_NoP_Bars -rstarcara) /noise_NoP_Bars)  if prob1>prob2
replace RPMCARAleft = 1-normal((rhatCARA_NoP_Bars -rstarcara) /noise_NoP_Bars) if prob1<prob2

*Collapse the dataset at the subject and choice pair level
collapse choiceleft pbarksubj* idleft idright RUMCRRAleft RUMCARAleft RPMCARAleft RPMCRRAleft, by(subj idpair)

*Calculate absolute mean error
gen diff=.
 local j = 1
          while `j' < 61{	
 local i = 1
          while `i' < 10{	
 local k = 1
          while `k' < 10{
capture: replace diff = choiceleft-pbarksubj`j'left`i'right`k' if idleft==`i' & idright==`k'
local k = `k' + 1
            }   		
local i = `i' + 1
            }  
local j = `j' + 1
            }  			
gen diffRUMCRRA = choiceleft-RUMCRRAleft
gen diffRUMCARA = choiceleft-RUMCARAleft	
gen diffRPMCRRA = choiceleft-RPMCRRAleft
gen diffRPMCARA = choiceleft-RPMCARAleft

gen distCRRANopressureBar = abs(diffRUMCRRA)
gen distCARANopressureBar = abs(diffRUMCARA)
gen distRPMCRRANopressureBar = abs(diffRPMCRRA)
gen distRPMCARANopressureBar = abs(diffRPMCARA)
gen distNopressureBar = abs(diff)


collapse distNopressureBar distCRRANopressureBar  distCARANopressureBar distRPMCRRANopressureBar distRPMCARANopressureBar, by(subj)
sum distNopressureBar distCRRANopressureBar  distCARANopressureBar distRPMCRRANopressureBar distRPMCARANopressureBar

save Th3NoPressureBar, replace
*Adapt the program for the other 4 conditions.
* Th3TimePressureBars
* Th3TimePressurePies
* Th3NoPressurePies
